package org.etherframework.tests.features.contextual
{
	import org.etherframework.core.EtherConfig;
	import org.etherframework.tests.features.access.dependencies.TopModuleDependency;
	import org.etherframework.tests.features.contextual.configs.HierarchicalConfig;
	import org.etherframework.tests.features.contextual.contexts.AnyContext;
	import org.etherframework.tests.features.contextual.dependencies.ChildModuleDependency;
	import org.etherframework.tests.features.contextual.dependencies.ModuleDependency;
	import org.etherframework.tests.features.contextual.dependencies.NonContextDependency;
	import org.etherframework.tests.utils.ModuleHierarchy;
	import org.etherframework.tests.utils.TestUtility;
	import org.fluint.uiImpersonation.UIImpersonator;
	import org.hamcrest.assertThat;
	import org.hamcrest.object.notNullValue;

	public class when_using_contextual_dependencies
	{	
		private static var _testUtility:TestUtility = new TestUtility();
		private static var _hiearchy:ModuleHierarchy;
		
		[BeforeClass]
		public static function setUpBeforeClass():void
		{
			const configs:Vector.<EtherConfig> = new Vector.<EtherConfig>();
			configs.push(new HierarchicalConfig(), new HierarchicalConfig(), new HierarchicalConfig()); 
			_hiearchy = _testUtility.createHierarchy(configs);
			UIImpersonator.addChild(_hiearchy.module.view);
			
			_hiearchy.module.dependencies.addClass(NonContextDependency);
			_hiearchy.module.dependencies.contextual(new AnyContext()).addClass(TopModuleDependency);
			_hiearchy.next().module.dependencies.contextual(new AnyContext()).addClass(ModuleDependency);
			_hiearchy.next().next().module.dependencies.contextual(new AnyContext()).addClass(ChildModuleDependency);
		}
		
		[AfterClass]
		public static function tearDownAfterClass():void
		{
			UIImpersonator.removeAllChildren();
			_testUtility.dispose();
		}
		
		[Test(order=1)]
		public function it_should_be_resolved():void
		{
			var error:Error;
			
			assertThat(_hiearchy.module.ether.dependencies.getInstance(NonContextDependency), notNullValue());
			assertThat(_hiearchy.module.ether.contextual(new AnyContext()).dependencies.getInstance(TopModuleDependency), notNullValue());
			assertThat(_hiearchy.next().module.ether.contextual(new AnyContext()).dependencies.getInstance(ModuleDependency), notNullValue());
			assertThat(_hiearchy.next().next().module.ether.contextual(new AnyContext()).dependencies.getInstance(ChildModuleDependency), notNullValue());
			
			try {
				_hiearchy.module.ether.dependencies.getInstance(TopModuleDependency);
			} catch (e:Error) {error = e;}
			assertThat(error, notNullValue());
			error = null;
			
			try {
				_hiearchy.next().module.ether.dependencies.getInstance(ModuleDependency);
			} catch (e:Error) {error = e;}
			assertThat(error, notNullValue());
			error = null;
			
			try {
				_hiearchy.next().next().module.ether.dependencies.getInstance(ChildModuleDependency);
			} catch (e:Error) {error = e;}
			assertThat(error, notNullValue());
		}
		
		[Test(order=2)]
		public function it_should_be_resolved_in_default_context_through_custom():void
		{
			assertThat(_hiearchy.module.ether.contextual(new AnyContext()).dependencies.getInstance(NonContextDependency), notNullValue());
			assertThat(_hiearchy.next().module.ether.contextual(new AnyContext()).dependencies.getInstance(NonContextDependency), notNullValue());
			assertThat(_hiearchy.next().next().module.ether.contextual(new AnyContext()).dependencies.getInstance(NonContextDependency), notNullValue());
		}
		
		
	}
}